package com.dianping.cat.configuration.server.transform;

import com.dianping.cat.configuration.server.IEntity;
import com.dianping.cat.configuration.server.IVisitor;
import com.dianping.cat.configuration.server.entity.ConsumerConfig;
import com.dianping.cat.configuration.server.entity.Domain;
import com.dianping.cat.configuration.server.entity.HarfsConfig;
import com.dianping.cat.configuration.server.entity.HdfsConfig;
import com.dianping.cat.configuration.server.entity.LongConfig;
import com.dianping.cat.configuration.server.entity.Property;
import com.dianping.cat.configuration.server.entity.Server;
import com.dianping.cat.configuration.server.entity.ServerConfig;
import com.dianping.cat.configuration.server.entity.StorageConfig;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

































public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  58 */   public DefaultXmlBuilder() { this(false); }



/*  62 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  66 */     this.m_compact = compact;
/*  67 */     this.m_sb = sb;
/*  68 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  72 */     entity.accept(this.m_visitor);
/*  73 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  77 */     this.m_level--;

/*  79 */     indent();
/*  80 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  84 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  88 */     if (value == null) {
/*  89 */       return null;
    }

/*  92 */     String str = toString(value);
/*  93 */     int len = str.length();
/*  94 */     StringBuilder sb = new StringBuilder(len + 16);

/*  96 */     for (int i = 0; i < len; i++) {
/*  97 */       char ch = str.charAt(i);

/*  99 */       switch (ch) {
        case '<':
/* 101 */           sb.append("&lt;");
          break;
        case '>':
/* 104 */           sb.append("&gt;");
          break;
        case '&':
/* 107 */           sb.append("&amp;");
          break;
        case '"':
/* 110 */           if (!text) {
/* 111 */             sb.append("&quot;");
            break;
          }
        default:
/* 115 */           sb.append(ch);
          break;
      }

    }
/* 120 */     return sb.toString();
  }

  protected void indent() {
/* 124 */     if (!this.m_compact) {
/* 125 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 126 */         this.m_sb.append("   ");
      }
    }
  }


/* 132 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 136 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 140 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 144 */     indent();

/* 146 */     this.m_sb.append('<').append(name);

/* 148 */     int len = nameValues.length;

/* 150 */     for (int i = 0; i + 1 < len; i += 2) {
/* 151 */       Object attrName = nameValues[i];
/* 152 */       Object attrValue = nameValues[i + 1];

/* 154 */       if (attrValue != null) {
/* 155 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 159 */     if (dynamicAttributes != null) {
/* 160 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 161 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 165 */     if (text != null && closed) {
/* 166 */       this.m_sb.append('>');
/* 167 */       this.m_sb.append(escape(text, true));
/* 168 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 170 */       if (closed) {
/* 171 */         this.m_sb.append('/');
      } else {
/* 173 */         this.m_level++;
      }

/* 176 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 182 */     if (value instanceof String)
/* 183 */       return (String)value; 
/* 184 */     if (value instanceof Collection) {
/* 185 */       Collection<Object> list = (Collection)value;
/* 186 */       StringBuilder sb = new StringBuilder(32);
/* 187 */       boolean first = true;

/* 189 */       for (Object item : list) {
/* 190 */         if (first) {
/* 191 */           first = false;
        } else {
/* 193 */           sb.append(',');
        }

/* 196 */         if (item != null) {
/* 197 */           sb.append(item);
        }
      }

/* 201 */       return sb.toString();
/* 202 */     }  if (value.getClass().isArray()) {
/* 203 */       int len = Array.getLength(value);
/* 204 */       StringBuilder sb = new StringBuilder(32);
/* 205 */       boolean first = true;

/* 207 */       for (int i = 0; i < len; i++) {
/* 208 */         Object item = Array.get(value, i);

/* 210 */         if (first) {
/* 211 */           first = false;
        } else {
/* 213 */           sb.append(',');
        }

/* 216 */         if (item != null) {
/* 217 */           sb.append(item);
        }
      }

/* 221 */       return sb.toString();
    }

/* 224 */     return String.valueOf(value);
  }


  public void visitConsumer(ConsumerConfig consumer) {
/* 229 */     startTag("consumer", null, new Object[0]);

/* 231 */     if (consumer.getLongConfig() != null) {
/* 232 */       consumer.getLongConfig().accept(this.m_visitor);
    }

/* 235 */     endTag("consumer");
  }



/* 240 */   public void visitDomain(Domain domain) { startTag("domain", true, null, new Object[] { "name", domain.getName(), "url-threshold", domain.getUrlThreshold(), "sql-threshold", domain.getSqlThreshold(), "service-threshold", domain.getServiceThreshold() }); }




/* 245 */   public void visitHarfs(HarfsConfig harfs) { startTag("harfs", true, null, new Object[] { "id", harfs.getId(), "max-size", harfs.getMaxSize(), "server-uri", harfs.getServerUri(), "base-dir", harfs.getBaseDir() }); }




/* 250 */   public void visitHdfs(HdfsConfig hdfs) { startTag("hdfs", true, null, new Object[] { "id", hdfs.getId(), "max-size", hdfs.getMaxSize(), "server-uri", hdfs.getServerUri(), "base-dir", hdfs.getBaseDir() }); }



  public void visitLongConfig(LongConfig longConfig) {
/* 255 */     startTag("long-config", null, new Object[] { "default-url-threshold", Integer.valueOf(longConfig.getDefaultUrlThreshold()), "default-sql-threshold", Integer.valueOf(longConfig.getDefaultSqlThreshold()), "default-service-threshold", Integer.valueOf(longConfig.getDefaultServiceThreshold()) });

/* 257 */     if (!longConfig.getDomains().isEmpty()) {
/* 258 */       for (Domain domain : longConfig.getDomains().values()) {
/* 259 */         domain.accept(this.m_visitor);
      }
    }

/* 263 */     endTag("long-config");
  }



/* 268 */   public void visitProperty(Property property) { startTag("property", true, null, new Object[] { "name", property.getName(), "value", property.getValue() }); }



  public void visitServer(Server server) {
/* 273 */     startTag("server", null, new Object[] { "id", server.getId() });

/* 275 */     if (!server.getProperties().isEmpty()) {
/* 276 */       startTag("properties");

/* 278 */       for (Property property : server.getProperties().values()) {
/* 279 */         property.accept(this.m_visitor);
      }

/* 282 */       endTag("properties");
    }

/* 285 */     if (server.getStorage() != null) {
/* 286 */       server.getStorage().accept(this.m_visitor);
    }

/* 289 */     if (server.getConsumer() != null) {
/* 290 */       server.getConsumer().accept(this.m_visitor);
    }

/* 293 */     endTag("server");
  }


  public void visitServerConfig(ServerConfig serverConfig) {
/* 298 */     startTag("server-config", null, new Object[0]);

/* 300 */     if (!serverConfig.getServers().isEmpty()) {
/* 301 */       for (Server server : serverConfig.getServers().values()) {
/* 302 */         server.accept(this.m_visitor);
      }
    }

/* 306 */     endTag("server-config");
  }


  public void visitStorage(StorageConfig storage) {
/* 311 */     startTag("storage", null, new Object[] { "local-base-dir", storage.getLocalBaseDir(), "max-hdfs-storage-time", Integer.valueOf(storage.getMaxHdfsStorageTime()), "local-report-storage-time", Integer.valueOf(storage.getLocalReportStorageTime()), "local-logivew-storage-time", Integer.valueOf(storage.getLocalLogivewStorageTime()), "har-mode", Boolean.valueOf(storage.isHarMode()), "upload-thread", Integer.valueOf(storage.getUploadThread()) });

/* 313 */     if (!storage.getHdfses().isEmpty()) {
/* 314 */       for (HdfsConfig hdfs : storage.getHdfses().values()) {
/* 315 */         hdfs.accept(this.m_visitor);
      }
    }

/* 319 */     if (!storage.getHarfses().isEmpty()) {
/* 320 */       for (HarfsConfig harfs : storage.getHarfses().values()) {
/* 321 */         harfs.accept(this.m_visitor);
      }
    }

/* 325 */     if (!storage.getProperties().isEmpty()) {
/* 326 */       startTag("properties");

/* 328 */       for (Property property : storage.getProperties().values()) {
/* 329 */         property.accept(this.m_visitor);
      }

/* 332 */       endTag("properties");
    }

/* 335 */     endTag("storage");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/server/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */